<template>
  <view class="wrapper">
    <!-- 订单状态 -->
    <view class="tabs">
      <text class="active">全部</text>
      <text>待付款</text>
      <text>已付款</text>
      <text>退款/退货</text>
    </view>
    <!-- 订单 -->
    <scroll-view class="orders" scroll-y>
      <!-- 遍历全部订单 -->
      <view class="item" :key="order.order_number" v-for="order in orders">
        <!-- 当前订单下=》商品 -->
        <block :key="prd.goods_id" v-for="prd in order.goods">
          <!-- 商品图片 -->
          <image class="pic" :src="prd.goods_small_logo" />
          <!-- 商品信息 -->
          <view class="meta">
            <view class="name">{{prd.goods_name}}</view>
            <view class="price">
              <text>￥</text>
              {{prd.goods_price}}
              <text>.00</text>
            </view>
            <view class="num">x{{prd.goods_number}}</view>
          </view>
        </block>
        <!-- 总价 -->
        <view class="amount">共{{order.total_count}}件商品 总计: ￥{{order.total_price}}(含运费0.00)</view>
        <!-- 其它 -->
        <view class="extra">
          订单号: {{order.order_number}}
          <button
            @click="payOrder(order.order_number)"
            size="mini"
            type="primary"
          >支付</button>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
export default {
  data () {
    return {
      // 订单列表
      orders: []
    }
  },
  onLoad () {
    this.getOrderList()
  },
  methods: {
    // 支付订单
    /**
     * order_number 当前支付的订单编号
     */
    async payOrder (order_number) {
      const { msg, data } = await this.request({
        url: '/my/orders/req_unifiedorder',
        method: 'post',
        header: {
          Authorization: uni.getStorageSync('hm_token')
        },
        data: {
          order_number
        }
      })

      console.log(msg, data)
      if (msg.status === 200) {
        // 支付成功
        // 1. 获取支付必须的参数
        let _pay = data.pay
        // 2. 调用微信支付窗口
        console.log(_pay)
        const [err, dataPay] = await uni.requestPayment(_pay)
        console.log('支付信息：', err, dataPay)
        if (!err) {
          uni.showToast({
            title: '支付成功！',
            duration: 2000
          })
        }
      } else {
        // 支付失败
        uni.showToast({
          title: msg.msg,
          duration: 2000
        })
      }

    },
    // 获取所有订单数据
    async getOrderList () {
      // 是否登录=》跳到登录
      if (!uni.getStorageSync('hm_token')) {
        return uni.showToast({
          title: '请登录！',
          duration: 2000,
          success () {
            // 跳转到登录
            uni.navigateTo({ url: '/packone/auth/index' })
          }
        })
      }
      // 获取订单列表
      const { msg, data } = await this.request({
        url: '/my/orders/all',
        header: {
          Authorization: uni.getStorageSync('hm_token')
        },
        data: {
          // 所有订单
          type: 1
        }
      })
      if (msg.status === 200) {
        console.log(data)
        this.orders = data.orders
      }
    }
  },

};
</script>

<style scoped lang="scss">
.tabs {
  display: flex;
  height: 96rpx;
  line-height: 96rpx;
  background-color: #fff;
  box-shadow: 0 4rpx 10rpx #ccc;

  text {
    flex: 1;
    text-align: center;
    font-size: 27rpx;
    color: #333;

    &.active {
      color: #ea4451;
    }
  }
}

.orders {
  width: 100%;
  background-color: #f4f4f4;

  position: absolute;
  top: 97rpx;
  bottom: 0;
}

.item {
  padding: 30rpx 20rpx 0;
  margin-top: 16rpx;
  background-color: #fff;

  .pic {
    width: 200rpx;
    height: 200rpx;
    float: left;
  }

  .meta {
    height: 200rpx;
    margin-left: 230rpx;
    font-size: 27rpx;
    color: #333;
    position: relative;
  }

  .name {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .price {
    position: absolute;
    bottom: 0;

    color: #ea4451;
    font-size: 33rpx;

    text {
      font-size: 22rpx;
    }
  }

  .num {
    position: absolute;
    bottom: 0;
    right: 20rpx;
    color: #333;
  }

  .amount {
    text-align: right;
    padding: 20rpx;
    font-size: 24rpx;
    border-top: 1rpx solid #eee;
    border-bottom: 1rpx solid #eee;
    margin-top: 20rpx;
    color: #999;
  }

  .extra {
    padding: 30rpx;
    font-size: 24rpx;
    color: #999;
    position: relative;

    button {
      position: absolute;
      right: 20rpx;
      font-size: 24rpx;
      margin-top: -10rpx;
    }
  }
}
</style>
